﻿using System;
using System.Security.Cryptography;
using System.Text;

namespace XStudio.XCRF.Utilities.MD5Encryption
{
    public static class MD5Extension
    {
        /// <summary>
        /// 将指定字符串进行MD5 32位加密
        /// </summary>
        /// <param name="input">输入字符串</param>
        /// <returns>生成的32位MD5码</returns>
        public static string MD5Encode32(this string input)
        {
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            byte[] InBytes = Encoding.GetEncoding("GB2312").GetBytes(input);
            byte[] OutBytes = md5.ComputeHash(InBytes);
            string output = "";
            for (int i = 0; i < OutBytes.Length; i++)
                output += OutBytes[i].ToString("X2");
            return output;
        }

        /// <summary>
        /// 将指定字符串进行MD5 16位加密
        /// </summary>
        /// <param name="input">输入字符串</param>
        /// <returns>生成的16位MD5码</returns>
        public static string MD5Encode16(this string input)
        {
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            string output = BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(input)), 4, 8);
            output = output.Replace("-", "");
            return output;
        } 
    }
}
